/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "highchart", "valueUtil","ajaxServ",  "bizProto","sysmanMain","btnServ","formServ","highchartTheme","commonSelect"],
		function($,  highchart, valueUtil,ajaxServ, bizProto,sysmanMain,btnServ,formServ,highchartTheme,commonSelect) {

			'use strict';

			var _bizProto,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,_buttonConfig,
				//部门选择框字段
				_defaultConfig ={},
				
				chartConfig ={
					chart: {
			            plotBackgroundColor: null,
			            plotBorderWidth: null,
			            plotShadow: false
			        },
			        credits:{
			        	enabled:false
			        },
			        title: {
			            text: '异常情况'
			        },
			        tooltip: {
			            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
			        },
			        legend:{
			        	labelFormat:'{name}: <b>{y}</b>次 , <b>{percentage:.1f}%</b>'			        	
			        },
			        plotOptions: {
			            pie: {
			                allowPointSelect: true,
			                cursor: 'pointer',
			                dataLabels: {
			                    enabled: false,
			                    distance:-20,
			                    format: '<b>{point.name}</b>:{point.y}件'
			                },
			                style: {
		                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
		                    },
		                    showInLegend: true
			            }
			        }
				},
				
				//url define
//				dutiesStatisticAllUrl = '/medical/medicalRecipel/query/getStaticInfoAll.action'
				dutiesStatisticAllUrl = '/medicalStatistic/query/getChartStatistic.action',
				
				_colors = [  "#99cc00","#ff0033","#99ccff"],
				
				rootid = 'panel-medical-chart'	
			
			var public_method = {
				
				
				init:function(_options){
					
					 Highcharts.getOptions().colors = Highcharts.map(_colors, function (color) {
				        return {
				            radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
				            stops: [
				                [0, color],
				                [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
				            ]
				        };
				    });
					var _startDate = $('#' +rootid+' #outhospitalat_start')
						
					var _endDate = $('#' +rootid+' #outhospitalat_end')
					if(_startDate.length>0){
						var curDate = new Date();
						
						var _tmp = valueUtil.getDateValue({source:curDate,format:'yyyy-MM-dd'})
						$(_endDate).datebox('setValue',_tmp);							
						
						
						curDate.setDate(1);
						
						_tmp = valueUtil.getDateValue({source:curDate,format:'yyyy-MM-dd'})
						$(_startDate).datebox('setValue',_tmp);
					}
					private_method.init(_options);
				}
				
			}

			var private_method = {
				init : function(_options) {
					if(window.console)
						console.info('medicalChart modules gets ready and been loaded.');
					
					$.extend(_defaultConfig,_options)
					_bizProto = new bizProto(_options)
					
					_buttonConfig={					
						"执行查询":private_method.initElement
					}
					private_method.bindBtnEvent()
					private_method.initElement(_bizProto.pageDoc)
					
					$.messager.progress('close');
				},				
				bindBtnEvent:function(){
					
					var	btn = $('#' +rootid +' a.function-btn')
					
					// 绑定按钮事件
					
					//判断按钮是否存在
					if(btn.length>0){
						$(btn).unbind('click');
						$(btn).bind('click', private_method.function_btn_listenner)	
					}		
				},
				// 功能按钮监听函数
				function_btn_listenner : function(e) {
		
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
		
				},
				//初始化元素
				initElement:function(doc){
					
					var _data = private_method.getDateValue()
					
					ajaxServ.runAjax({
						url:_bizProto.rootPath + dutiesStatisticAllUrl,
						data:{condition:_data},
						type:'post',
						callback:private_method.initChart					
					})
					
					
				},
				getDateValue:function(){
					
					var _form = $('#'+ rootid +' form.condition-form ')
					var ret = formServ.getDataObj({
						rootId:rootid,
						form : $(_form)
					});
					return ret
				},
				initChart:function(data){
					private_method.initFeeChart(data.fee)
					private_method.initDrugChart(data.drug)
					private_method.initCheckChart(data.check)
					private_method.initOtherChart(data.other)
				},
				
				initFeeChart:function(data){
					var _container =$(_bizProto.pageDoc).find('#container-fee')
					var _param ={
						title: {
				            text: '收费异常'
				        },
						series: [{
							allowPointSelect:true,
				            type: 'pie',
				            name: '所占比例',
				            data: data,
				            events:{click:private_method.chartClick}
				        }]
					}
					
					_param =$.extend({},chartConfig,_param)
					$(_container).highcharts(_param);
				},
				
				initDrugChart:function(data){
					var _container =$(_bizProto.pageDoc).find('#container-drug')
					var _param ={
						title: {
				            text: '用药异常'
				        },
						series: [{
							allowPointSelect:true,
				            type: 'pie',
				            name: '所占比例',
				            data: data,
				            events:{click:private_method.chartClick}
				        }]
					}
					
					_param =$.extend({},chartConfig,_param)
					$(_container).highcharts(_param);
				},
			
				//初始化全部业务图表
				initCheckChart:function(data){
					var _container =$(_bizProto.pageDoc).find('#container-check')
					
					var _param ={
						title: {
				            text: '检查异常'
				        },
						series: [{
							allowPointSelect:true,
				            type: 'pie',
				            name: '所占比例',
				            data: data,
				            events:{click:private_method.chartClick}
				        }]
					}
					
					_param =$.extend({},chartConfig,_param)
					$(_container).highcharts(_param);
					
				},
				
				//初始化全部业务图表
				initOtherChart:function(data){
					var _container =$(_bizProto.pageDoc).find('#container-other')
					
					var _param ={
						title: {
				            text: '其他异常'
				        },
						series: [{
							allowPointSelect:true,
				            type: 'pie',
				            name: '所占比例',
				            data: data,
				            events:{click:private_method.chartClick}
				        }]
					}
					
					_param =$.extend({},chartConfig,_param)
					$(_container).highcharts(_param);
					
				},
				

			
				chartClick:function(e){
					//var _series =this.getSelectedSeries();
//					alert('series: ' +e.point.name +' selected. value: ' + e.point.y);
					var _param
					if(e.point.issue){						
						_param= {issue:e.point.issue}
						
						
					}
					
					$.extend(_param,private_method.getDateValue());
					
					
					var _passedParam = {initQueryParams:{condition:_param}}
					if(e.point.issue ==='biz_summary'){
						_passedParam.queryMethod = 'getBizListDomain';
						sysmanMain.setModuConf('medicalBizInfo',_passedParam)
						var _rootPath = sysmanMain.getRootPath();
						commonSelect.medicalBizInfoSelect({rootPath:_rootPath})
					}else{
					
						_passedParam.queryMethod = 'getRecipelListDomain';
						sysmanMain.setModuConf('medicalInfo',_passedParam)
						
						sysmanMain.medicalInfoSelect()
					}
				}
			}
			
			return public_method;

		});